<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\LikeCityList;
use app\model\User;
use think\Request;

class LikeCityListController
{
    protected $likeCityLisyModel;
    protected $userModel;

    public function __construct(){
        $this->likeCityLisyModel = new LikeCityList();
        $this->userModel = new User();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //获取传递过来的邮箱和id
        //添加喜欢的数据
        $email = $request->post('email');
        $like_city_list_id = $request->post('like_city_list_id');
        try {
            //        判断是哪一个用户
            $user_id = $this->userModel->where('email', $email)->value('id');
            $res = $this->likeCityLisyModel->save([
                'user_id' => $user_id,
                'like_city_list_id' => $like_city_list_id
            ]);
        }catch (\Exception $e){
            return  json([
                "message" => "判断错误",
                "code" => 400
            ]);
        }
        if($res){
            return json([
                "message" => "增加成功！",
                "code" => 200,
                "data" => "增加成功！"
            ]);
        }else{
            return json([
                "message" => "增加失败！",
                'code' => 400
            ]);
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request,$id)
    {
        //删除喜欢的数据
        $email = $request->delete('email');
        try {
            //        判断是哪一个用户
            $user_id = $this->userModel->where('email', $email)->value('id');
//        删除对应的数据
            $res = $this->likeCityLisyModel->where('user_id', $user_id)->where("like_city_list_id",$id)->delete();
        }catch (\Exception $e){
            return json([
                "message" => "发生了错误",
                "code" => 400
            ]);
        }
        if ($res) {
            return json([
                "message"=>"删除成功！",
                "code"=>200,
                "data" =>"删除成功"
            ]);
        }else{
            return json([
                "message"=> "删除失败",
                "code"=> 400
            ]);
        }

    }
}
